import api from '../../api';
import { message } from 'antd';
import styleCommom from '../../css/colors.js';

export default {
  namespace: 'bondDeviationDetailM',
  state: {
    mainIncomObj: {}, //主营业务表格
    mainIncomPie: [], //主营业务饼图
    marketObj: {}, //流通
    comFinancialData: {}, //财务数据
    dateListCom: [], //财务日期列表
    dateListMainIncom: [], //主要收入日期列表
    valuationDetails: [], //估值详情数据
    recommendData: [], //推荐数据
    norecommendData: [], //不推荐数据
    series: undefined, // y轴数据
    xAxis: undefined, // x轴日期数组
    valuationtype: null,
    max1: null,
    min1: null,
    max2: null,
    min2: null,
    activeKey: '1', //当前tab页
    dataObj: {}, //顶部基本信息
    isConvertibleBond: false, //是否可转债
  },
  effects: {
    // 获取顶部基本信息
    *queryBasicInfoSnapshot({ payload, callback }, { call, put, select }) {
      const res = yield call(api.bondBaseInfo.queryBasicInfoSnapshot, payload);
      if (res) {
        if (res.code === 0) {
          yield put({
            type: 'setState',
            payload: {
              dataObj: res.data,
            },
          });
          callback(res.data || []);
        } else {
          message.error(res.msg || '查询债券信息失败!');
        }
      } else {
        message.error('查询债券信息失败!');
      }
    },

    //获取个券组关注列表
    *getSecurityGroups({ payload, callback }, { call, put }) {
      const res = yield call(api.bondBaseInfo.getSecurityGroups, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg);
      }
    },

    //获取主体组关注列表
    *getCompanyGroups({ payload, callback }, { call, put }) {
      const res = yield call(api.bondBaseInfo.getCompanyGroups, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg);
      }
    },

    //个券新增关注
    *updateSecurityGroups({ payload, callback }, { call, put }) {
      const res = yield call(api.bondBaseInfo.updateSecurityGroups, payload);
      if (res && res.code == 0) {
        callback(true);
      } else {
        message.error('添加失败');
      }
    },

    //主体新增关注
    *updateCompanyGroups({ payload, callback }, { call, put }) {
      const res = yield call(api.bondBaseInfo.updateCompanyGroups, payload);
      if (res && res.code == 0) {
        callback(true);
      } else {
        message.error('添加失败');
      }
    },

    //估值详情数据导出
    *valuationDetailsExport({ payload }, { call, put, select }) {
      const res = yield call(api.bondBaseInfo.valuationDetailsExport, payload);
      if (res && res.code == 0) {
      }
    },

    //估值详情数据
    *valuationDetails({ payload }, { call, put, select }) {
      let recommendData = [];
      let norecommendData = [];
      let netvaluation = [];
      let evalutedyield = [];
      let xAxis = [];
      let valuationtype = '';

      const res = yield call(api.bondBaseInfo.valuationDetails, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'setState',
          payload: {
            valuationDetails: res.data,
          },
        });
        res.data.dataSource.map((item) => {
          if (item.recommend == '推荐') {
            recommendData.push(item);
            netvaluation.push(item.netvaluation); //净价
            evalutedyield.push(item.evalutedyield); //估值收益率
            xAxis.push(item.dataDt); //时间
            valuationtype = item.valuationtype;
          } else {
            norecommendData.push(item);
            valuationtype = item.valuationtype;
          }
        });

        let series = [
          { name: '估值收益率', data: evalutedyield.reverse(), yAxisIndex: 0 },
          { name: '净价', data: netvaluation.reverse(), yAxisIndex: 1 },
        ];
        let max2 = Math.max.apply(null, evalutedyield);
        let min2 = Math.min.apply(null, evalutedyield);
        let max1 = Math.max.apply(null, netvaluation);
        let min1 = Math.min.apply(null, netvaluation);

        yield put({
          type: 'setState',
          payload: {
            recommendData: recommendData,
            norecommendData: norecommendData,
            xAxis: xAxis.reverse(),
            series: series,
            valuationtype: valuationtype,
            max2: max2 ? max2 : null,
            min2: min2 ? min2 : null,
            max1: max1 ? max1 : null,
            min1: min1 ? min1 : null,
          },
        });
      }
    },

    // 获取基本信息
    *queryBondBasicInfoDetail({ payload, callback }, { call, put, select }) {
      const response = yield call(api.bondBaseInfo.queryBondBasicInfoDetail, payload);
      if (response && response.code == 0) {
        callback(response.data || {});
        if (response.data?.categoryNamei == '可转债') {
          yield put({ type: 'setState', payload: { isConvertibleBond: true } });
        } else {
          yield put({ type: 'setState', payload: { isConvertibleBond: false } });
        }
      } else {
        callback();
        message.error('查询债券基础信息失败!');
      }
    },

    // 获取财务数据
    *queryComFinancial({ payload, callback }, { call, put, select }) {
      const res = yield call(api.bondBaseInfo.queryComFinancial, payload);

      if (res) {
        if (res.code === 0) {
          yield put({
            type: 'setState',
            payload: {
              comFinancialData: res.data || {},
            },
          });
          if (Object.keys(res.data).length) {
            callback(res.data.ENDDATE);
          }
        } else {
          yield put({
            type: 'setState',
            payload: {
              comFinancialData: {},
            },
          });
          message.error(res.msg);
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            comFinancialData: {},
          },
        });
        message.error('查询财务数据失败');
      }
    },

    // 获取主营业务
    *queryMainIncom({ payload, callback }, { call, put, select }) {
      const res = yield call(api.bondBaseInfo.queryMainIncom, payload);
      if (res) {
        if (res.code === 0) {
          var mainIncomPie = [];
          res.data.dataSource.map((item, key) => {
            item.color = styleCommom.echart[key];
            mainIncomPie.push({
              value: item.percent,
              name: item.type,
            });
          });
          res.data.columns.unshift({
            title: '',
            key: 'color',
          });
          yield put({
            type: 'setState',
            payload: {
              mainIncomObj: res.data,
              mainIncomPie,
            },
          });
          if (res.data.dataSource.length) {
            callback(res.data.dataSource[0].ENDDATE);
          }
        } else {
          yield put({
            type: 'setState',
            payload: {
              mainIncomObj: {},
              mainIncomPie: [],
            },
          });
          message.error(res.msg);
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            mainIncomObj: {},
            mainIncomBar: [],
          },
        });
      }
    },

    // 获取流通市场
    *queryMarket({ payload, callback }, { call, put, select }) {
      const res = yield call(api.bondBaseInfo.queryMarket, payload);
      if (res) {
        if (res.code === 0) {
          yield put({
            type: 'setState',
            payload: {
              marketObj: res.data,
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              marketObj: {},
            },
          });
          message.error(res.msg);
        }
      } else {
        yield put({
          type: 'setState',
          payload: {
            marketObj: {},
          },
        });
      }
    },

    // 获取债券日历
    *queryBondCalendar({ payload, callback }, { call, put, select }) {
      const response = yield call(api.bondBaseInfo.queryBondCalendar, payload);
      if (response && response.code == 0) {
        const obj = {
          issueDate: '发行日',
          accruedDate: '起息日',
          paymentDate: '缴款日',
          listingDate: '上市日',
          nextPaymentDate: '付息日',
          endDate: '到期日',
        };
        const arr = [];
        const tempObj = response.data || {};
        if (Object.keys(tempObj).length) {
          for (var key in obj) {
            if (tempObj[key]) {
              arr.push({
                name: obj[key],
                date: tempObj[key],
              });
            }
          }
        }
        callback(arr);
      } else {
        callback();
        message.error('查询债券日历失败!');
      }
    },

    // 获取债券发行信息
    *queryBondIssuerInfoDetail({ payload, callback }, { call, put, select }) {
      const response = yield call(api.bondBaseInfo.queryBondIssuerInfoDetail, payload);
      if (response && response.code == 0) {
        callback(response.data || {});
      } else {
        callback();
        message.error('查询债券发行信息失败!');
      }
    },

    //获取日期下拉列表
    *queryDateList({ payload }, { call, put, select }) {
      const { type } = payload;
      const res = yield call(api.bondBaseInfo.queryDateList, payload);
      if (res) {
        if (res.code === 0) {
          if (type == 'financialindicator') {
            yield put({
              type: 'setState',
              payload: {
                dateListCom: res.data || [],
              },
            });
          } else {
            yield put({
              type: 'setState',
              payload: {
                dateListMainIncom: res.data || [],
              },
            });
          }
        } else {
          if (type == 'financialindicator') {
            yield put({
              type: 'setState',
              payload: {
                dateListCom: [],
              },
            });
          } else {
            yield put({
              type: 'setState',
              payload: {
                dateListMainIncom: [],
              },
            });
          }
          message.error(res.msg);
        }
      } else {
        if (type == 'financialindicator') {
          yield put({
            type: 'setState',
            payload: {
              dateListCom: [],
            },
          });
        } else {
          yield put({
            type: 'setState',
            payload: {
              dateListMainIncom: [],
            },
          });
        }
        message.error('查询日期列表失败!');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
